import apiClient from './client'
// 后端支付接口返回使用 Map<String, Object> 简化，这里用精确结构代替 unknown
export interface CreatePaymentResponse {
  paymentId: string
  orderId: number
  paymentMethod: string
  paymentUrl: string
  amount: number
  status: string
}

export interface PaymentStatusResponse {
  paymentId: string
  status: string
}

export interface RefundResponseData {
  refundId: string
  paymentId: string
  refundAmount: number
  refundReason: string
  status: string
}

export const paymentService = {
  // ===== 支付管理模块 (/api/v1/payment) =====

  // 创建支付请求
  createPayment: (request: { orderId: number; paymentMethod: string; returnUrl?: string }) =>
    apiClient.post<CreatePaymentResponse>('/api/v1/payment/create', undefined, { params: request }),

  // 查询支付状态
  queryPaymentStatus: (paymentId: string) =>
    apiClient.get<PaymentStatusResponse>(`/api/v1/payment/status/${paymentId}`),

  // 支付回调处理
  handlePaymentCallback: (callbackData: string) =>
    apiClient.post<string>('/api/v1/payment/callback', { data: callbackData }),

  // 取消支付订单
  cancelPayment: (paymentId: string) =>
    apiClient.post('/api/v1/payment/cancel', undefined, { params: { paymentId } }),

  // 申请退款
  applyRefund: (refundRequest: { paymentId: string; refundAmount: number; refundReason: string }) =>
    apiClient.post<RefundResponseData>('/api/v1/payment/refund', undefined, {
      params: refundRequest,
    }),

  // 查询退款状态
  queryRefundStatus: (refundId: string) =>
    apiClient.get<RefundResponseData>(`/api/v1/payment/refund-status/${refundId}`),
}

export default paymentService
